package com.gg.verification.controller;

import com.gg.verification.pojo.Result;
import com.gg.verification.pojo.VerificationPo;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/java/api/v1")
public class Verification {
    @Autowired
    private VerificationPo verificationPo;
    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/verification")
    public Result verification(@RequestParam String userAgent, String code) {
//        Map<String, Object> codeMap = verificationPo.getCode();
//        HttpSession session = request.getSession();
//        String sessionId = session.getId();
//        // 先检查 codeMap 是否为 null
//        if (codeMap == null) {
//            return Result.error(500, "验证码未初始化");
//        }
//        System.out.println("本次验证码为："+codeMap.get(sessionId));
//
//        Object v_code = codeMap.get(sessionId);
//        if (v_code == null || !v_code.equals(code))
//            return Result.error(400,"验证码错误");
        RBucket<String> bucket = redissonClient.getBucket("captcha:" + userAgent);
        String storedCaptcha = bucket.get();
        if (storedCaptcha == null) {
            // 验证码已过期，进行相应处理，例如返回错误信息给前端
            return Result.error(400, "验证码已过期");

        }
        // 验证码未过期，继续验证
        if (!code.equals(storedCaptcha))
            return Result.error(400, "验证码错误");

        return Result.success("验证通过");
    }
}
